﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace SSLVerifier.API.ModelObjects {
	public class TreeNode<T> {
		private readonly T _value;
		private readonly ObservableCollection<TreeNode<T>> _children = new ObservableCollection<TreeNode<T>>();

		public TreeNode(T value) {
			_value = value;
		}
		public TreeNode<T> this[int i] {
			get { return _children[i]; }
		}
		public TreeNode<T> Parent { get; private set; }
		public T Value { get { return _value; } }
		public ObservableCollection<TreeNode<T>> Children {
			get { return _children; }
		}

		public TreeNode<T> AddChild(T value) {
			var node = new TreeNode<T>(value) { Parent = this };
			_children.Add(node);
			return node;
		}
		public TreeNode<T>[] AddChildren(params T[] values) {
			return values.Select(AddChild).ToArray();
		}
		public bool RemoveChild(TreeNode<T> node) {
			return _children.Remove(node);
		}
		public void Traverse(Action<T> action) {
			action(Value);
			foreach (var child in _children)
				child.Traverse(action);
		}
		public IEnumerable<T> Flatten() {
			return new[] { Value }.Union(_children.SelectMany(x => x.Flatten()));
		}
	}
}
